Skip to content

fix(codex): dedupe repeated token snapshots#878

Open
albincsergo wants to merge 1 commit intoryoppippi:mainfrom
albincsergo:fix/codex-double-counting
Open

fix(codex): dedupe repeated token snapshots#878
albincsergo wants to merge 1 commit intoryoppippi:mainfrom
albincsergo:fix/codex-double-counting

Conversation

@albincsergo
Copy link

@albincsergo albincsergo commented Mar 6, 2026

Summary

  • prefer diffing cumulative total_token_usage when available
  • avoid counting duplicate token_count snapshots that repeat last_token_usage
  • add a regression test for repeated rate-limit snapshots

Verification

  • pnpm --filter @ccusage/codex typecheck
  • pnpm --filter @ccusage/codex test

Repro

A duplicated Codex snapshot sequence counted 2540 tokens before this change and 1440 tokens after it.

Summary by CodeRabbit

  • Bug Fixes

    • Refined token usage tracking to reliably calculate usage metrics from cumulative data when available, improving consistency across duplicate events.
  • Tests

    • Added test coverage validating proper deduplication and token calculation in edge cases.

@coderabbitai
Copy link

coderabbitai bot commented Mar 6, 2026

📝 Walkthrough

Walkthrough

Modified data-loader.ts to always compute per-event usage deltas from cumulative totals when available, falling back to last-usage snapshots only when totals are null. Added deduplication logic and unit tests for duplicate total snapshot handling.

Changes

Cohort / File(s) Summary
Data Loader Usage Calculation
apps/codex/src/data-loader.ts
Refactored usage calculation logic to prioritize totalUsage when available, compute deltas via subtractRawUsage, and include deduplication handling for duplicate total snapshots. Added unit test validating deduplication behavior with repeated last usage and duplicate totals.

Possibly related issues

Poem

🐰 A hop, a bound, a fix so neat,
Totals first, that can't be beat!
No double counts shall hop away,
Deduped totals save the day! 🎉


🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: fixing deduplication of repeated token snapshots in the codex data loader to prevent double-counting.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
apps/codex/src/data-loader.ts (1)

295-306: ⚠️ Potential issue | 🟠 Major

Advance previousTotals when falling back to last_token_usage.

After Line 302 uses lastUsage, previousTotals is not advanced. If a later event resumes total_token_usage, Line 300 diffs against stale/null totals and re-emits already-counted usage. Example: a last=100 event followed by a total=150 event currently emits 100 and 150, not 100 and 50. Please also add a regression for a last-only -> total transition.

Suggested fix
+function addRawUsage(base: RawUsage | null, delta: RawUsage): RawUsage {
+	return {
+		input_tokens: (base?.input_tokens ?? 0) + delta.input_tokens,
+		cached_input_tokens: (base?.cached_input_tokens ?? 0) + delta.cached_input_tokens,
+		output_tokens: (base?.output_tokens ?? 0) + delta.output_tokens,
+		reasoning_output_tokens:
+			(base?.reasoning_output_tokens ?? 0) + delta.reasoning_output_tokens,
+		total_tokens: (base?.total_tokens ?? 0) + delta.total_tokens,
+	};
+}
...
 let raw: RawUsage | null = null;
 if (totalUsage != null) {
 	// Prefer cumulative totals when available. Codex can emit duplicate token_count
 	// snapshots (e.g. with different rate-limit buckets) where last_token_usage repeats.
 	// Diffing totals collapses those duplicates into zero-delta events.
 	raw = subtractRawUsage(totalUsage, previousTotals);
-} else {
-	raw = lastUsage;
-}
-
-if (totalUsage != null) {
 	previousTotals = totalUsage;
+} else {
+	raw = lastUsage;
+	if (lastUsage != null) {
+		previousTotals = addRawUsage(previousTotals, lastUsage);
+	}
 }

Based on learnings, "Use payload.info.total_token_usage as cumulative totals and payload.info.last_token_usage as per-turn delta; when only cumulative totals exist, compute delta by subtracting previous totals".

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@apps/codex/src/data-loader.ts` around lines 295 - 306, When falling back to
last_token_usage (the lastUsage branch) we must advance previousTotals so future
total_token_usage diffs against an updated cumulative baseline; update the
branch that sets raw = lastUsage to also set previousTotals = previousTotals ?
addRawUsage(previousTotals, lastUsage) : lastUsage (or equivalent) so the
per-turn delta is incorporated into previousTotals, and add a regression test
covering a last-only -> total transition (e.g., last=100 then total=150 should
emit 100 then 50). Reference variables/functions: lastUsage, totalUsage,
previousTotals, subtractRawUsage (and use or add an addRawUsage helper if
needed).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@apps/codex/src/data-loader.ts`:
- Around line 295-306: When falling back to last_token_usage (the lastUsage
branch) we must advance previousTotals so future total_token_usage diffs against
an updated cumulative baseline; update the branch that sets raw = lastUsage to
also set previousTotals = previousTotals ? addRawUsage(previousTotals,
lastUsage) : lastUsage (or equivalent) so the per-turn delta is incorporated
into previousTotals, and add a regression test covering a last-only -> total
transition (e.g., last=100 then total=150 should emit 100 then 50). Reference
variables/functions: lastUsage, totalUsage, previousTotals, subtractRawUsage
(and use or add an addRawUsage helper if needed).

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: bbd53fd3-a9c8-4070-a336-f9c4e6781005

📥 Commits

Reviewing files that changed from the base of the PR and between 0adbb4f and 4736ded.

📒 Files selected for processing (1)
  • apps/codex/src/data-loader.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant